Imports System.Text
Imports System.Security.Cryptography
Imports System.IO

Public Module tools

    Public Function EncryptPassword(ByVal plainTextPassword As String) As String
        Dim sha As New SHA1Managed
        Dim encoder As New ASCIIEncoding
        Dim bytes As Byte() = encoder.GetBytes(plainTextPassword)
        Dim encryptedBytes As Byte() = sha.ComputeHash(bytes)

        Dim builder As New StringBuilder
        Dim lth As Integer = encryptedBytes.Length
        For lp As Integer = 0 To lth - 1
            builder.Append(String.Format("{0,2:x}", encryptedBytes(lp)).Replace(" ", "0"))
        Next
        Return builder.ToString
    End Function

    Public Function Field(ByVal sourceString As String, ByVal delimiter As Char, ByVal pos As Integer) As String
        Dim parts() As String = sourceString.Split(delimiter)
        If pos > parts.Length Then
            Return ""
        Else
            Return parts(pos - 1)
        End If
    End Function

    Public Function GetFileHash(ByVal filename As String) As String
        Dim fs As FileStream = IO.File.OpenRead(filename)
        Dim m5 As MD5 = MD5.Create
        Dim byteHash() As Byte = m5.ComputeHash(fs)
        Dim base64Hash As String = Convert.ToBase64String(byteHash)
        fs.Close()
        Return base64Hash
    End Function

    Public Function GetXElement(ByVal xmlString As String) As XElement
        Dim sr As New StringReader(xmlString)
        
        Try
            Return XElement.Load(sr)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Module

